#ifndef ACCEPTOR_H
#define ACCEPTOR_H


#include "Common.h"

void make_nonblocking(int fd) {
    fcntl(fd, F_SETFL, O_NONBLOCK);
}

class Acceptor {
public:
	int listen_fd;
	int listen_port;

	Acceptor(int port): listen_port(port) {
	    
	    listen_fd = socket(AF_INET, SOCK_STREAM, 0);
	    make_nonblocking(listen_fd);

	    struct sockaddr_in serv_addr;

	    memset(&serv_addr, 0, sizeof(serv_addr));

	    serv_addr.sin_family = AF_INET;
	    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	    serv_addr.sin_port = htons(listen_port);

	    int on = 1;
    	setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

	    int rt1 = bind(listen_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
	    if (rt1 < 0) {
	    	printf("bind failed\n");
	        // error(1, errno, "bind failed ");
	    }

	    int rt2 = listen(listen_fd, 1024);
	    if (rt2 < 0) {
	    	printf("listen failed\n");
	        // error(1, errno, "listen failed ");
	    }
	}

};

#endif